package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Dictionary;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("dictionary")
public class DictionaryController {
    @Autowired
    private IDictionaryService dictionaryService;

    @RequiresPermissions(value={"dictionary:view","系统字典视图"},logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "dictionary/dictionary";
    }

    @RequiresPermissions(value={"dictionary:query","系统字典列表"},logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(QueryObject qo) {
        return dictionaryService.query(qo);
    }

    @RequiresPermissions(value={"dictionary:saveOrUpdate","系统字典编辑/新增"},logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Dictionary entity) {
        try {
            dictionaryService.saveOrUpdate(entity);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }
    @RequiresPermissions(value={"dictionary:delete","系统字典删除"},logical = Logical.OR)
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            dictionaryService.deleteByPrimaryKey(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

}
